package com.zz.framework.core.ext

import android.app.Application
import android.content.Context
import com.zz.framework.core.BaseApp

fun Any?.toString(): String {
    if (this == null) return ""
    return toString()
}

fun Any?.getNonNullString(): String {
    if (this == null) return ""
    return toString()
}

fun Long?.getNonNullLong(): Long {
    if (this == null) return 0
    return this
}

fun Long?.add(value: Long): Long {
    return if (this == null) {
        1
    } else {
        this + value
    }
}

fun Int?.getNonNullInt(): Int {
    if (this == null) return 0
    return this
}

fun Int?.add(value: Int): Int {
    return if (this == null) {
        1
    } else {
        this + value
    }
}

fun Double?.getNonNullDouble(): Double {
    if (this == null) return 0.0
    return this
}

fun Boolean?.getNonNullBoolean(): Boolean {
    if (this == null) return false
    return this
}

fun Int?.reduce(value: Int): Int {
    return if (this == null) {
        -1
    } else {
        this - value
    }
}

fun Long?.getNonNullValue(): Long {
    if (this == null) return 0
    return this
}


fun Application.dip2px(dp: Float): Int {
    val scale = applicationContext.getResources().getDisplayMetrics().density
    return (dp * scale + 0.5f).toInt()
}

fun Int.dp2px(): Int {
    val scale = BaseApp.context.getResources().getDisplayMetrics().density
    return (toFloat() * scale + 0.5f).toInt()
}
fun Int.dp(): Int {
    val scale = BaseApp.context.getResources().getDisplayMetrics().density
    return (toFloat() * scale + 0.5f).toInt()
}

fun Int.px2dp(): Int {
    val scale = BaseApp.context.getResources().getDisplayMetrics().density
    return (toFloat() / scale + 0.5f).toInt()
}

fun Int.sp2px(): Int {
    return (toFloat() * BaseApp.context.getResources().displayMetrics.scaledDensity + 0.5f).toInt()
}

fun Int.px2sp(): Int {
    return (toFloat() / BaseApp.context.getResources().displayMetrics.scaledDensity + 0.5f).toInt()
}



fun Float.dp2px(): Int {
    return (this * BaseApp.context.getResources().displayMetrics.density + 0.5f).toInt()
}

fun Float.px2dp(): Int {
    return (this / BaseApp.context.getResources().displayMetrics.density + 0.5f).toInt()
}

fun Float.sp2px(): Int {
    return (this * BaseApp.context.getResources().displayMetrics.scaledDensity + 0.5f).toInt()
}

fun Float.px2sp(): Int {
    return (this / BaseApp.context.getResources().displayMetrics.scaledDensity + 0.5f).toInt()
}



fun getVersion(context: Context): String {
    var appVersion: String
    try {
        appVersion = context.packageManager.getPackageInfo(context.getPackageName(), 0).versionName
    } catch (e: Exception) {
        appVersion = "1.0.0"
    }

    return appVersion
}


fun Any.getString(stringRes:Int):String{
    return BaseApp.context.getString(stringRes)
}

//fun Any.getString(stringRes:Int, vararg formatArgs:Any):String{
//    return BaseApp.context.getString(stringRes,formatArgs)
//}



